/* 
 * File:   Animation.cpp
 * Author: agu
 * 
 * Created on 24 de mayo de 2014, 18:51
 */
#include <iostream>
#include "client_Animation.h"
using std::cout;
using std::endl;

Animation::Animation(SDL_Renderer* renderer, const string path, int x, int y) :
texture(renderer, path, x, y) {

    //Set sprite clips
    this->frames = 3;
    int sizeFrame = 32;
    
    for (int i = 0; i < this->frames; i++) {
        SDL_Rect pos;
        pos.x = i*sizeFrame;
        pos.y = 0;
        pos.w = sizeFrame;
        pos.h = sizeFrame;
        this->spritesDown.push_back(pos);
    }

    for (int i = 0; i < this->frames; i++) {
        SDL_Rect pos;
        pos.x = i*sizeFrame;
        pos.y = sizeFrame;
        pos.w = sizeFrame;
        pos.h = sizeFrame;
        this->spritesLeft.push_back(pos);
    }

    for (int i = 0; i < this->frames; i++) {
        SDL_Rect pos;
        pos.x = i*sizeFrame;
        pos.y = sizeFrame * 2;
        pos.w = sizeFrame;
        pos.h = sizeFrame;
        this->spritesRight.push_back(pos);
    }


    for (int i = 0; i < this->frames; i++) {
        SDL_Rect pos;
        pos.x = i*sizeFrame;
        pos.y = sizeFrame * 3;
        pos.w = sizeFrame;
        pos.h = sizeFrame;
        this->spritesUp.push_back(pos);
    }
    this->currentSprite = this->spritesDown;
    this->currentFrame = 0;
}

Animation::~Animation() {
}

void Animation::render() {
    //este render seria el de Texture
    Uint32 ticks = SDL_GetTicks();
    this->currentFrame = (ticks / 100) % this->frames;
    this->texture.render(this->texture.getX(), this->texture.getY(),
            &this->currentSprite[this->currentFrame]);
    this->currentFrame++;
}

void Animation::updatePosition(int x, int y) {
    //Nueva posicion
    this->texture.setPosition(x, y);
}

void Animation::getPosition(int& x, int& y) {
    x = this->texture.getX();
    y = this->texture.getY();
}

void Animation::render(int iniX, int iniY) {
    int difX = this->texture.getX() - iniX;
    int difY = this->texture.getY() - iniY;
    if (difX != 0) {
        for (int i = iniX; i < this->texture.getX(); i++) {
            Uint32 ticks = SDL_GetTicks();
            this->currentFrame = (ticks / 100) % this->frames;
            this->texture.render(i, this->texture.getY(),
                    &this->currentSprite[this->currentFrame]);
            this->currentFrame++;
        }
    } else {
        if (difY != 0) {
            for (int i = iniY; i < this->texture.getY(); i++) {
                Uint32 ticks = SDL_GetTicks();
                this->currentFrame = (ticks / 100) % this->frames;
                this->texture.render(this->texture.getX(), this->texture.getY(),
                        &this->currentSprite[this->currentFrame]);
                this->currentFrame++;
            }
        } else {
            Uint32 ticks = SDL_GetTicks();
            this->currentFrame = (ticks / 100) % this->frames;
            this->texture.render(this->texture.getX(), this->texture.getY(),
                    &this->currentSprite[this->currentFrame]);
            this->currentFrame++;
        }
    }

}

void Animation::moveUp() {
    this->currentSprite = this->spritesUp;
}

void Animation::moveDown() {
    this->currentSprite = this->spritesDown;
}

void Animation::moveLeft() {
    this->currentSprite = this->spritesLeft;
}

void Animation::moveRight() {
    this->currentSprite = this->spritesRight;
}

void Animation::updateDirection(string codeDirection, string up, string down, string left, string right) {
    if (codeDirection == up) {
        moveUp();
    } else {
        if(codeDirection == down){
            moveDown();
        }else{
            if(codeDirection == left){
                moveLeft();
            }else{
                if(codeDirection == right){
                    moveRight();
                }
            }
        }
    }
}